package com.sansan.qiangji.controller.base;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.BaseWageAccountAddForm;
import com.sansan.qiangji.form.base.BaseWageAccountEditForm;
import com.sansan.qiangji.form.base.BaseWageAccountListForm;
import com.sansan.qiangji.realize.base.BaseWageAccountReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("/baseWageAccount")
@Slf4j
@CrossOrigin()
public class BaseWageAccountController {


    @Autowired
    private BaseWageAccountReal baseWageAccountReal;


    @PostMapping("/addBaseWageAccount")
    public ResultVO<Object> addBaseWageAccount(@Valid @RequestBody BaseWageAccountAddForm baseWageAccountAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【工资基础新增】参数不正确，baseWageAccountAddForm={}",baseWageAccountAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        baseWageAccountReal.addBaseWageAccount(baseWageAccountAddForm);
        return ResultVOUtil.success(null);
    }


    @PostMapping("/getBaseWageAccountList")
    public ResultVO<Object> getBaseWageAccountList(@Valid @RequestBody BaseWageAccountListForm form, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【工资基础查询】参数不正确，baseWageAccountAddForm={}",form);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(baseWageAccountReal.baseWageAccountList(form));
    }


    @PostMapping("/editBaseWageAccount")
    public ResultVO<Object> editBaseWageAccount(@Valid @RequestBody BaseWageAccountEditForm baseWageAccountEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【工资基础修改】参数不正确，baseWageAccountAddForm={}",baseWageAccountEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        baseWageAccountReal.editBaseWageAccount(baseWageAccountEditForm);
        return ResultVOUtil.success(null);
    }


    @PostMapping("/deleteBaseWageAccount")
    public ResultVO<Object> deleteBaseWageAccount(@RequestParam(value = "baseWageAccountId") Integer baseWageAccountId){
        baseWageAccountReal.deleteBaseWageAccount(baseWageAccountId);
        return ResultVOUtil.success(null);
    }

}
